package com.xzkj.xiaozhengblog;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class main {
    GetConfig getconfig = new GetConfig();
    private String pathClass = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
    private String pathsql;
    {
        try {
            pathsql = java.net.URLDecoder.decode(pathClass + "com\\xzkj\\xiaozhengblog\\config\\sqlConfig.ini", "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    private String JDBC_DRIVER = getconfig.getConfig("JDBC_DRIVER", pathsql);
    private String DB_URL = getconfig.getConfig("DB_URL", pathsql);
    private String SQL_USER = getconfig.getConfig("SqlUserName", pathsql);
    private String SQL_PASS = getconfig.getConfig("SqlUserPassWord", pathsql);
    public main(){
        //注册JDBC驱动
        try {
            Class.forName(JDBC_DRIVER);
            System.out.println("数据库驱动注册成功!");
            //获取Connection对象
            try {
                datamysql.conn = DriverManager.getConnection(DB_URL, SQL_USER, SQL_PASS);
                System.out.println("获取Connection对象成功!");
                String sql = "CREATE TABLE `user` (\n" +
                        "  `id` int(11) NOT NULL AUTO_INCREMENT,\n" +
                        "  `username` varchar(255) DEFAULT NULL,\n" +
                        "  `userpass` varchar(255) DEFAULT NULL,\n" +
                        "  `email` varchar(255) DEFAULT NULL,\n" +
                        "  `qq` varchar(255) DEFAULT NULL,\n" +
                        "  `ip` varchar(255) DEFAULT NULL,\n" +
                        "  PRIMARY KEY (`id`)\n" +
                        ") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
                PreparedStatement ps = null;
                ps = datamysql.conn.prepareStatement(sql);
                ps.execute();
                sql = "CREATE TABLE `artlist` (\n" +
                        "\t`id` INT ( 11 ) NOT NULL AUTO_INCREMENT COMMENT '唯一标识',\n" +
                        "\t`username` VARCHAR ( 255 ) DEFAULT NULL COMMENT '发布用户',\n" +
                        "\t`tab` VARCHAR ( 255 ) DEFAULT NULL COMMENT '标题',\n" +
                        "\t`dateart` datetime DEFAULT NULL COMMENT '发布时间',\n" +
                        "\t`reading` VARCHAR ( 255 ) DEFAULT NULL COMMENT '阅读',\n" +
                        "\t`collection` VARCHAR ( 255 ) DEFAULT NULL COMMENT '收藏',\n" +
                        "\t`text` LONGTEXT COMMENT '内容',\n" +
                        "PRIMARY KEY ( `id` ) \n" +
                        ") ENGINE = INNODB DEFAULT CHARSET = utf8;";
                ps = datamysql.conn.prepareStatement(sql);
                ps.execute();
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("获取Connection对象失败!");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("注册驱动失败!");
        }
    }

    /** 获取Connection对象
     * @return Connection对象*/

    public static Connection getConn(){
        return datamysql.conn;
    }
}
